<?php
// +----------------------------------------------------------------------
// | najing [ 通用后台管理系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020 http://www.najingquan.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 救火队队长
// +----------------------------------------------------------------------

namespace app\cardeadmin\controller;

use app\common\controller\AdminController;
use app\common\model\Manage as ManageModel;
use app\common\model\OperationLog as LogModel;
use app\common\model\UserLog;
use think\facade\View;

/**
 * 操作日志管理
 * Class Operation
 * @package app\admin\controller
 */
class OperationLog extends AdminController
{
    /**
     * 操作日志列表
     * @author 救火队队长
     */
    public function index()
    {
        $logModel = new LogModel();
        if ($this->request->isAjax()) {
            $request = input('param.');
            return $logModel->tableData($request);
        }
        return  View::fetch();
    }

    /**
     * 获取最新的5条操作日志
     * @author 救火队队长
     */
    public function getLastLog(){
        $logModel = new LogModel();
        $request['limit'] = 5;//最近5条数据
        $logInfo = $logModel->tableData($request);
        return $logInfo;
    }

    /**
     * 获取最新的5条登陆日志
     * @author 救火队队长
     */
    public function userLog(){
        $logModel = new UserLog();
        if ($this->request->isAjax())
        {
            $request = input('param.');
            $request['limit'] = 5;//最近5条数据
            $userInfo = $logModel->tableData($request);

            foreach ($userInfo['data'] as &$v){
                $manageModel = new ManageModel();
                $data = $manageModel->field('id,username,nickname,mobile')->where(['id' => $v['user_id']])->find();
                $v['username'] = $data['username'];
            }
            return $userInfo;
        }
        return View::fetch('user_list');
    }


    /**
     * 删除日志
     * @author 救火队队长
     */
    public function delLog()
    {
        $result = [
            'status' => false,
            'msg' => '失败',
            'data' => ''
        ];

        $ids = input('ids/a', []);
        if(!$ids)
        {
            return $result;
        }
        $logModel = new LogModel();
        $res = $logModel->where([['id','in',$ids]])->delete();
        if($res !== false)
        {
            $result['msg'] = '删除成功';
            $result['status'] = true;
        }
        return $result;
    }

}